package database;

import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;

public class Delete extends JInternalFrame {
	
	Connection conn;
	private JButton btnNewButton;
	private JLabel lblNewLabel;
	private JLabel lblNewLabel_1;
	private JLabel lblEa;
	private JTextField serial;
	private JTextField code;
	private JTextField ea;
	private JLabel status;
	private JButton btnNewButton_1;
	
	public Delete(Connection c) {
		super("삭제", true, true, true, true);
		setBounds(100, 100, 626, 279);
		this.setDefaultCloseOperation(HIDE_ON_CLOSE);  // 창을 끄고 메인에서 다시 선택했을때 뜨게하는것
		this.conn = c;
		getContentPane().setLayout(null);
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getLblNewLabel_1());
		getContentPane().add(getLblEa());
		getContentPane().add(getSerial());
		getContentPane().add(getCode());
		getContentPane().add(getEa());
		getContentPane().add(getStatus());
		getContentPane().add(getBtnNewButton_1());
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("검색");
			btnNewButton.setFont(new Font("나눔고딕", Font.PLAIN, 12));
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					int s = Integer.parseInt(serial.getText());
					String sql = "select * from ptbl where serial = ?";
										
					try {
						PreparedStatement pstmt = conn.prepareStatement(sql);
						pstmt.setInt(1, s);
						//pstmt.executeUpdate();
						ResultSet rs = pstmt.executeQuery();
						
						if(rs.next()){
							code.setText(rs.getString("code"));
							ea.setText("" + rs.getInt("ea"));
						}else{
							status.setText("삭제할 데이터가 없습니다.");
							serial.requestFocus();
						}
						
					} catch (Exception ex) {
						ex.printStackTrace();
					}
				}
			});
			btnNewButton.setBounds(389, 51, 97, 23);
		}
		return btnNewButton;
	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("serial");
			lblNewLabel.setFont(new Font("나눔고딕", Font.PLAIN, 12));
			lblNewLabel.setBounds(122, 54, 57, 15);
		}
		return lblNewLabel;
	}
	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("code");
			lblNewLabel_1.setFont(new Font("나눔고딕", Font.PLAIN, 12));
			lblNewLabel_1.setBounds(122, 83, 57, 15);
		}
		return lblNewLabel_1;
	}
	private JLabel getLblEa() {
		if (lblEa == null) {
			lblEa = new JLabel("ea");
			lblEa.setFont(new Font("나눔고딕", Font.PLAIN, 12));
			lblEa.setBounds(122, 115, 57, 15);
		}
		return lblEa;
	}
	private JTextField getSerial() {
		if (serial == null) {
			serial = new JTextField();
			serial.setBounds(191, 51, 116, 21);
			serial.setColumns(10);
		}
		return serial;
	}
	private JTextField getCode() {
		if (code == null) {
			code = new JTextField();
			code.setBounds(191, 80, 116, 21);
			code.setColumns(10);
		}
		return code;
	}
	private JTextField getEa() {
		if (ea == null) {
			ea = new JTextField();
			ea.setBounds(191, 112, 116, 21);
			ea.setColumns(10);
		}
		return ea;
	}
	private JLabel getStatus() {
		if (status == null) {
			status = new JLabel("데이터를 선택해주세요");
			status.setBounds(12, 225, 586, 15);
		}
		return status;
	}
	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("삭제");
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					int s = Integer.parseInt(serial.getText());
					
					String sql = "delete from ptbl where serial = ?";
					
					try {
						PreparedStatement pstmt = conn.prepareStatement(sql);
						pstmt.setInt(1, s);
						
						pstmt.executeUpdate();
						pstmt.close();
						
						status.setText(serial.getText() + " 삭제되었습니다."); 
						
						serial.setText("");
						code.setText("");
						ea.setText("");
												
					} catch (Exception ex) {
						ex.printStackTrace();
					}
				}
			});
			btnNewButton_1.setFont(new Font("나눔고딕", Font.PLAIN, 12));
			btnNewButton_1.setBounds(265, 154, 97, 33);
		}
		return btnNewButton_1;
	}
}
